package com.m3958.firstgwt.server.service;

import java.io.StringWriter;
import java.util.Properties;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.m3958.firstgwt.server.utils.Osdetecter;

@Singleton
public class VelocityService {

	@Inject
	public VelocityService(SiteConfigService scs){
	    Properties p = new Properties();
	    p.setProperty("file.resource.loader.path", scs.getSiteRoot());
	    if(Osdetecter.isWindows()){
	    	p.setProperty("runtime.log", "e:/volocity.log");
	    }else{
	    	p.setProperty("runtime.log", "/usr/share/tomcat6/logs/velocity.log");
	    }
	    Velocity.init( p );
//	    ttt();
	}
	
	public boolean templateExists(String tplName){
		return Velocity.resourceExists(tplName);
	}

//	private void ttt() {
//        VelocityContext context = new VelocityContext();
//
//        context.put("name", "Velocity");
//        context.put("project", "Jakarta");
//
//        /* lets render a template */
//
//        StringWriter w = new StringWriter();
//
//        Velocity.mergeTemplate("testtemplate.vm","utf-8", context, w );
//        System.out.println(" template : " + w );
//
//        /* lets make our own string to render */
//
//        String s = "We are using $project $name to render this.";
//        w = new StringWriter();
//        Velocity.evaluate( context, w, "mystring", s );
//        System.out.println(" string : " + w );
//		
//	}
}
